package test02;

import java.io.*;

public class User implements Runnable {
    private File source;  // 源文件
    private File destination;  // 目标文件
    private long totalSize;  // 文件总大小
    private volatile long copiedSize = 0; // 已拷贝大小
    private volatile boolean isFinished = false; // 是否拷贝完成

    public User(File source, File destination) {
        this.source = source;
        this.destination = destination;
        this.totalSize = source.length(); // 获取文件总大小
    }

    public long getTotalSize() {
        return totalSize;
    }

    public long getCopiedSize() {
        return copiedSize;
    }

    public boolean isFinished() {
        return isFinished;
    }

    @Override
    public void run() {
        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(destination)) {
            byte[] buffer = new byte[1024]; // 8KB 缓冲区
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
                copiedSize += bytesRead; // 更新拷贝进度
                Thread.sleep(100); // 每200ms更新一次进度
            }
            isFinished = true; // 拷贝完成
            System.out.println("文件拷贝完成！");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
